---
title: "Cleaning Pew World's Muslims dataset"
---

# Load

```{r}
# load packages
  source("helper-packages.R")

# load wvs
  pewwm_raw <- 
    read_sav("../raw-data/y-multi-pew-worlds-muslims/2012-Pew-Religion-Worlds-Muslims_dataset.sav")
```

# Extract education variables and codes

```{r}
# # extract education vars
#   bla <- pewwm_raw %>% select(starts_with("q101"))
# 
# # package for variable labels
#   library(expss)
# 
# # function to extract labels
#   lab_fun <- 
#     function(varname = NULL) {
#      
#       var <- pewwm_raw %>% pull(get(varname))
#       
#       out <- as.data.frame(unlist(val_lab(var))) %>% 
#         rownames_to_column() %>% 
#         mutate(vname = varname) %>% 
#         rename(num = `unlist(val_lab(var))`)
#           
#     }
# 
# # map over edu labels and clean output
#   recode_out <- 
#     pmap_dfr(list(colnames(bla)), .f = lab_fun) %>% 
#     filter(!num %in% c(98,99)) %>%
#     filter(!rowname %in% c("Don’t know", "Don't know", "Refused")) %>% 
#     mutate(code = paste0(vname, " == ", num, " ~ ", '"', num, ". ", rowname, ' [Primary]",')) %>% 
#     write.csv("todelete.csv")
```

# Clean data

```{r}
# declare dates
  pewwm_dates <- 
    tribble(
      ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
      "Afghanistan", "Nov. 27, 2011", "Dec. 17, 2011",
      "Albania", "Oct. 24, 2011", "Nov. 13, 2011",
      "Algeria", "Nov. 8, 2012", "Nov. 24, 2012", ### odd; appears surveyed later and subsequently appended to this dataset
      "Azerbaijan", "Dec. 4, 2011", "Dec. 22, 2011",
      "Bangladesh", "Nov. 21, 2011", "Feb. 5, 2012",
      "Bosnia", "Nov. 3, 2011", "Nov. 20, 2011",
      "Egypt", "Nov. 14, 2011", "Dec. 18, 2011",
      "Indonesia", "Oct. 28, 2011", "Nov. 19, 2011",
      "Iran", "Feb. 24, 2012", "May. 3, 2012",
      "Iraq", "Nov. 4, 2011", "Dec. 1, 2011",
      "Jordan", "Nov. 3, 2011", "Dec. 3, 2011",
      "Kazakhstan", "Nov. 24, 2011", "Dec. 17, 2011",
      "Kosovo", "Dec. 16, 2011", "Jan. 20, 2012",
      "Kyrgyzstan", "Jan. 31, 2012", "Feb. 25, 2012",
      "Lebanon", "Nov. 14, 2011", "Dec. 8, 2011",
      "Malaysia", "Nov. 4, 2011", "Jan. 25, 2012",
      "Morocco", "Nov. 3, 2011", "Dec. 1, 2011",
      "Niger", "Dec. 5, 2011", "Dec. 16, 2011",
      "Pakistan", "Nov. 10, 2011", "Nov. 30, 2011",
      "Palestine", "Dec. 4, 2011", "Jan. 2, 2012",
      "Russia", "Oct. 27, 2011", "Dec. 2, 2011",
      "Tajikistan", "Dec. 28, 2011", "Jan. 21, 2012",
      "Thailand", "Nov. 12, 2011", "Jan. 8, 2012",
      "Tunisia", "Nov. 10, 2011", "Dec. 7, 2011",
      "Turkey", "Nov. 18, 2011", "Dec. 19, 2011",
      "Uzbekistan", "Feb. 2, 2012", "Feb. 12, 2012") %>% 
    mutate(
      resp_interview_start_date = as.Date(resp_interview_start_date, "%b. %d, %Y"),
      resp_interview_end_date = as.Date(resp_interview_end_date, "%b. %d, %Y"))

# clean
  pewwm_clean <- 
    pewwm_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Pew: The World’s Muslims---Religion, Politics and Society",
        
      # round number (character vector, title case)  
        resp_round = "",      
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "pewrsr.ch/3LXoZiu",

      # survey mode (in-person/phone/internet)
        resp_survey_mode = "in-person",

      # country (character vector; list of countries as written in original source)
        resp_country_original = to_character(COUNTRY),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = NA) %>% 
        left_join(
          pewwm_dates, by = "resp_country_common") %>% 
        mutate(    
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
      # respondent's religion (character vector that corresponds to master list)
        # helpful resource: http://gizemarikan.com/wp-content/uploads/2016/11/BJPOLS-SUPP-APPENDIX.pdf
        resp_religion = "Muslim", # this was asked as a screening question (Q28rec)      

      # respondent's religion (character vector that corresponds to master list)
        resp_denomination = to_character(Q31rec),  
    
      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age = 
          case_when(
            Q96 == 9 ~ NA_character_,
            Q96 == 98 ~ NA_character_,
            Q96 == 99 ~ NA_character_,
            TRUE ~ as.character(Q96)),
   
      # respondent's education level
        resp_education_original = # morocco's education variable is missing
          case_when(
            Q101AFG == 1 ~ "1. No formal education [No education]",
            Q101AFG == 2 ~ "2. Completed 5 grades or less [Primary]",
            Q101AFG == 3 ~ "3. Completed 6, 7, or 8 grades [Primary]",
            Q101AFG == 4 ~ "4. Completed 9 or 10 grades [Primary]",
            Q101AFG == 5 ~ "5. Completed 11 or 12 grades [Primary]",
            Q101AFG == 6 ~ "6. Some college or university but not graduated [Primary]",
            Q101AFG == 7 ~ "7. College or university graduate [College]",
            Q101AFG == 8 ~ "8. Post-graduate [College]",
            
            Q101ALB == 1 ~ "1. No formal education or less than lower elementary school (has not completed 4 grades) [No education]",
            Q101ALB == 2 ~ "2. Lower elementary school (completed 4 grades) [Primary]",
            Q101ALB == 3 ~ "3. Lower secondary school (completed 9 grades) [Primary]",
            Q101ALB == 4 ~ "4. Completed upper secondary—general [Primary]",
            Q101ALB == 5 ~ "5. Completed upper secondary—vocational [Primary]",
            Q101ALB == 6 ~ "6. Some university [Primary]",
            Q101ALB == 7 ~ "7. University degree [College]",
            Q101ALB == 8 ~ "8. Post-graduate degree [College]",
            
            Q101ALG == 1 ~ "1. Illiterate [No education]",
            Q101ALG == 2 ~ "2. Primary [Primary]",
            Q101ALG == 3 ~ "3. Intermediate [Primary]",
            Q101ALG == 4 ~ "4. Secondary [Primary]",
            Q101ALG == 5 ~ "5. University [College]",
            Q101ALG == 6 ~ "6. Post-University [College]",
            Q101ALG == 7 ~ "7. Vocational Education [Primary]",
            Q101ALG == 8 ~ "8. Religious Education [Primary]",
            
            Q101AZE == 1 ~ "1. Less than base-general education (did not complete 9 grades under new system or 8 grades under old system) [No education]",
            Q101AZE == 2 ~ "2. Base-general education (completed 9 grades under new system or 8 grades under old system) [Primary]",
            Q101AZE == 3 ~ "3. General secondary education (completed 11 grades under new system or 10 grades under old system) [Primary]",
            Q101AZE == 4 ~ "4. Special secondary education (completed 2 or 3 years of college) [Primary]",
            Q101AZE == 5 ~ "5. Higher education (completed 4 or more years of university studies under new system or 5 or more years under old system) [College]",
            
            Q101BAN == 1 ~ "1. No formal education [No education]",
            Q101BAN == 2 ~ "2. Some primary education (has not completed class V) [No education]",
            Q101BAN == 3 ~ "3. Primary education (completed class V) [Primary]",
            Q101BAN == 4 ~ "4. Some secondary education (has not completed SSC) [Primary]",
            Q101BAN == 5 ~ "5. Secondary education (completed SSC) [Primary]",
            Q101BAN == 6 ~ "6. Some Higher Secondary Education (has not completed HSC) [Primary]",
            Q101BAN == 7 ~ "7. Higher Secondary Education (completed HSC) [Primary]",
            Q101BAN == 8 ~ "8. Some university education, but did not complete degree or graduate [Primary]",
            Q101BAN == 9 ~ "9. University degree (e.g. Bachelor degree/License degree) [College]",
            Q101BAN == 10 ~ "10. Post-graduate degree (e.g. PhD, MBA) [College]",
            Q101BAN == 11 ~ "11. Doctoral/Engineering [College]",
            Q101BAN == 12 ~ "12. Vocational/Diploma [Primary]",
            
            Q101BOS == 1 ~ "1. No formal education [No education]",
            Q101BOS == 2 ~ "2. Completed 1, 2, or 3 classes of primary school [No education]",
            Q101BOS == 3 ~ "3. Completed 4, 5, 6, or 7 classes of primary school [Primary]",
            Q101BOS == 4 ~ "4. Completed 8 classes of primary school [Primary]",
            Q101BOS == 5 ~ "5. Vocational school (completed 1 or 2 years, short program) [Primary]",
            Q101BOS == 6 ~ "6. Vocational school (completed 3 years) [Primary]",
            Q101BOS == 7 ~ "7. Vocational school (completed 4 years) [Primary]",
            Q101BOS == 8 ~ "8. Secondary school (completed 4 years) [Primary]",
            Q101BOS == 9 ~ "9. High school, university first degree (completed 2 years) [Primary]",
            Q101BOS == 10 ~ "10. Completed university [College]",
            Q101BOS == 11 ~ "11. Master’s degree or equivalent [College]",
            Q101BOS == 12 ~ "12. Doctorate [College]",
            
            Q101EGY == 1 ~ "1. No formal education [No education]",
            Q101EGY == 2 ~ "2. Incomplete elementary education (completed less than 6 grades) [No education]",
            Q101EGY == 3 ~ "3. Complete elementary education (completed 6 grades) [Primary]",
            Q101EGY == 4 ~ "4. Incomplete preparatory education (completed 7 or 8 grades) [Primary]",
            Q101EGY == 5 ~ "5. Complete preparatory education (completed 9 grades) [Primary]",
            Q101EGY == 6 ~ "6. Incomplete intermediate/secondary education (completed 10 or 11 grades) [Primary]",
            Q101EGY == 7 ~ "7. Complete intermediate/secondary education (completed 12 grades) [Primary]",
            Q101EGY == 8 ~ "8. Above intermediate (completed 2-year college) [Primary]",
            Q101EGY == 9 ~ "9. University and above [College]",
            
            Q101IDN == 1 ~ "1. No formal education [No education]",
            Q101IDN == 2 ~ "2. Incomplete grade school (completed 1-5 grades) [No education]",
            Q101IDN == 3 ~ "3. Complete grade school (completed 6 grades) [Primary]",
            Q101IDN == 4 ~ "4. Incomplete junior high school (completed 1-2 secondary grades) [Primary]",
            Q101IDN == 5 ~ "5. Complete junior high school (completed 3 secondary grades) [Primary]",
            Q101IDN == 6 ~ "6. Incomplete high school (completed 1-2 high school grades) [Primary]",
            Q101IDN == 7 ~ "7. Complete high school (completed 3 high school grades) [Primary]",
            Q101IDN == 8 ~ "8. Some university (has not completed a degree) [Primary]",
            Q101IDN == 9 ~ "9. University education, with degree [College]",
            
            Q101IRN == 1 ~ "1. Less than primary/illiterate [No education]",
            Q101IRN == 2 ~ "2. Primary or Elementary school completed [Primary]",
            Q101IRN == 3 ~ "3. Partial Middle School [Primary]",
            Q101IRN == 4 ~ "4. Middle school (grades 6,7,8) completed [Primary]",
            Q101IRN == 5 ~ "5. Partial high school education [Primary]",
            Q101IRN == 6 ~ "6. Students currently enrolled in secondary school [Primary]",
            Q101IRN == 7 ~ "7. Diploma (high school diploma) [Primary]",
            Q101IRN == 8 ~ "8. Pre-university [Primary]",
            Q101IRN == 9 ~ "9. Students currently enrolled in a college/university [Primary]",
            Q101IRN == 10 ~ "10. Some university completed but no degree [Primary]",
            Q101IRN == 11 ~ "11. Obtained license or BS degree [College]",
            Q101IRN == 12 ~ "12. MA, MPhil, PhD, or MD completed [College]",
            
            Q101IRQ == 1 ~ "1. None (illiterate, read-write only, no formal school) [No education]",
            Q101IRQ == 2 ~ "2. Primary [Primary]",
            Q101IRQ == 3 ~ "3. Intermediate [Primary]",
            Q101IRQ == 4 ~ "4. Secondary [Primary]",
            Q101IRQ == 5 ~ "5. Diploma [Primary]",
            Q101IRQ == 6 ~ "6. University Graduate [College]",
            Q101IRQ == 7 ~ "7. Post-graduate [College]",
            
            Q101JOR == 1 ~ "1. No formal education [No education]",
            Q101JOR == 2 ~ "2. Incomplete primary (completed less than 9 grades) [No education]",
            Q101JOR == 3 ~ "3. Complete primary (completed 9 grades) [Primary]",
            Q101JOR == 4 ~ "4. Incomplete secondary (completed 10 or 11 grades) [Primary]",
            Q101JOR == 5 ~ "5. Complete secondary (completed 12 grades) [Primary]",
            Q101JOR == 6 ~ "6. Intermediate diploma (completed 2 or 3 grades in community college) [Primary]",
            Q101JOR == 7 ~ "7. Bachelor and above [College]",
            
            Q101KAZ == 1 ~ "1. No formal education or incomplete primary education (completed less than 4 grades under new system or less than 3 grades [No education]",
            Q101KAZ == 2 ~ "2. Primary  education (completed 4 grades under new system or 3 grades under old system) [Primary]",
            Q101KAZ == 3 ~ "3. Base-general education (completed 9 grades under new system or 8 grades under old system) [Primary]",
            Q101KAZ == 4 ~ "4. General secondary education (completed 11 grades under new system or 10 grades under old system) [Primary]",
            Q101KAZ == 5 ~ "5. Incomplete special secondary education (completed 1 year of college) [Primary]",
            Q101KAZ == 6 ~ "6. Special secondary education (completed 2 or 3 years of college) [Primary]",
            Q101KAZ == 7 ~ "7. Incomplete higher education (completed 2 years of university studies under new system or 3 years under old system) [Primary]",
            Q101KAZ == 8 ~ "8. Higher education (completed 4 or more years of university studies under new system or 5 or more years under old system) [College]",
            
            Q101KOS == 1 ~ "1. No schooling [No education]",
            Q101KOS == 2 ~ "2. Completed 1, 2, or 3 years of elementary school [No education]",
            Q101KOS == 3 ~ "3. Completed 4 years of elementary school [No education]",
            Q101KOS == 4 ~ "4. Completed 5, 6, or 7 years of elementary school [Primary]",
            Q101KOS == 5 ~ "5. Primary (completed 8 years) [Primary]",
            Q101KOS == 6 ~ "6. Incomplete secondary (completed 9 or 10 years) [Primary]",
            Q101KOS == 7 ~ "7. Secondary (completed 11 or 12 years) [Primary]",
            Q101KOS == 8 ~ "8. Completed higher school [Primary]",
            Q101KOS == 9 ~ "9. Incomplete university [Primary]",
            Q101KOS == 10 ~ "10. Completed university or higher [College]",
            
            Q101KYR == 1 ~ "1. No formal education [No education]",
            Q101KYR == 2 ~ "2. Incomplete primary education (completed less than 4 grades under the new system or less than 3 grades under the old syst [No education]",
            Q101KYR == 3 ~ "3. Primary education (completed 4 grades under the new system or 3 grades under the old system) [Primary]",
            Q101KYR == 4 ~ "4. Base-general education (completed 9 grades under new system or 8 grades under old system) [Primary]",
            Q101KYR == 5 ~ "5. General secondary education (completed 11 grades under new system or 10 grades under old system) [Primary]",
            Q101KYR == 6 ~ "6. Special secondary education (completed 2 or 3 years of college) [Primary]",
            Q101KYR == 7 ~ "7. Incomplete higher education (completed 2 years of university studies under the new system or 3 years under the old syste [Primary]",
            Q101KYR == 8 ~ "8. Higher education (completed 4 or more years of university studies under new system or 5 or more years under old system) [College]",
            
            Q101LEB == 1 ~ "1. Incomplete elementary or less (0-7 years) (not enrolled/illiterate/preschool/read and write) [No education]",
            Q101LEB == 2 ~ "2. Complete elementary (8 years) [Primary]",
            Q101LEB == 3 ~ "3. Incomplete intermediate (9-11 years) [Primary]",
            Q101LEB == 4 ~ "4. Complete intermediate (12 years) [Primary]",
            Q101LEB == 5 ~ "5. Incomplete secondary (13-14 years) [Primary]",
            Q101LEB == 6 ~ "6. Complete secondary (15 years) [Primary]",
            Q101LEB == 7 ~ "7. Some university without degree [Primary]",
            Q101LEB == 8 ~ "8. University with degree [College]",
            
            Q101MAL == 1 ~ "1. No formal education [No education]",
            Q101MAL == 2 ~ "2. Primary school (began or completed Standard 1 to Standard 6 / UPSR) [Primary]",
            Q101MAL == 3 ~ "3. Lower secondary school (began or completed Form 1 to Form 3 / PMR / SRP / LCE) [Primary]",
            Q101MAL == 4 ~ "4. Upper secondary school (began or completed Form 4 to Form 5 / SPM / MCE / SC / GCE ‘O’ Level) [Primary]",
            Q101MAL == 5 ~ "5. Vocational / technical (began or completed vocational / technical school, e.g. SPVM / MCVE) [Primary]",
            Q101MAL == 6 ~ "6. Trade and technical skills institution  (began or completed studies in trade and technical skills institution, e.g. cert [Primary]",
            Q101MAL == 7 ~ "7. Post Secondary (began or completed post-secondary studies, e.g. STPM / STP / STA / 4THANAWI / HSC / GCE ‘A’ Level, diplo [Primary]",
            Q101MAL == 8 ~ "8. Tertiary (began or completed tertiary studies, e.g. post-diploma / college / polytechnic / university education) [College]",
            Q101MAL == 9 ~ "9. Post graduate (began or completed post graduate studies; e.g. PhD, Master) [College]",
            
            Q101NIG == 1 ~ "1. No formal education [No education]",
            Q101NIG == 2 ~ "2. Incomplete elementary (finished CI, CP, CE1, CE2, or CM1) [No education]",
            Q101NIG == 3 ~ "3. Complete elementary (finished CM2) [Primary]",
            Q101NIG == 4 ~ "4. Incomplete middle school (finished sixieme, cinquieme, or quatrieme) [Primary]",
            Q101NIG == 5 ~ "5. Complete middle school (finished troisieme) [Primary]",
            Q101NIG == 6 ~ "6. Incomplete high school/trade school/technical school (finished seconde or premiere) [Primary]",
            Q101NIG == 7 ~ "7. Complete high school/trade school/technical school (finished terminal) [Primary]",
            Q101NIG == 8 ~ "8. Incomplete first cycle of university (did not finish 2 or 3 years of university, DEUG or DUEL) [Primary]",
            Q101NIG == 9 ~ "9. Complete first cycle of university (finished 2 or 3 years of university, DEUG, or DUEL) [College]",
            Q101NIG == 10 ~ "10. Second cycle of university (finished 4 years of university studies) [College]",
            Q101NIG == 11 ~ "11. PhD [College]",
            
            Q101PAK == 1 ~ "1. No education at all [No education]",
            Q101PAK == 2 ~ "2. No formal education but can read/write simple Urdu [No education]",
            Q101PAK == 3 ~ "3. Incomplete primary education (completed less than 5 grades) [No education]",
            Q101PAK == 4 ~ "4. Complete primary education (completed 5 grades) [Primary]",
            Q101PAK == 5 ~ "5. Incomplete middle school (completed 6 or 7 grades) [Primary]",
            Q101PAK == 6 ~ "6. Complete middle school (completed 8 grades) [Primary]",
            Q101PAK == 7 ~ "7. Incomplete matric (completed 9 grades) [Primary]",
            Q101PAK == 8 ~ "8. Matriculation (completed 10 grades) [Primary]",
            Q101PAK == 9 ~ "9. Intermediate (completed 12 grades) [Primary]",
            Q101PAK == 10 ~ "10. Graduate (completed 14 years of studies) [College]",
            Q101PAK == 11 ~ "11. Post-graduate (completed 16 years of studies) [College]",
            
            Q101PAL == 1 ~ "1. No formal education [No education]",
            Q101PAL == 2 ~ "2. Incomplete elementary education (completed less than 5 grades under new system or less than 6 grades under old system) [No education]",
            Q101PAL == 3 ~ "3. Complete elementary education (completed 5 grades under new system or 6 grades under old system) [Primary]",
            Q101PAL == 4 ~ "4. Complete preparatory education (completed 10 grades under new system or 9 grades under old system) [Primary]",
            Q101PAL == 5 ~ "5. Complete secondary education (completed 12 grades) [Primary]",
            Q101PAL == 6 ~ "6. Diploma [Primary]",
            Q101PAL == 7 ~ "7. BA [College]",
            Q101PAL == 8 ~ "8. Masters [College]",
            Q101PAL == 9 ~ "9. PhD [College]",
            
            Q101RUS == 1 ~ "1. No formal education or less than primary education (completed 1-2 grades only) [No education]",
            Q101RUS == 2 ~ "2. Completed primary general education (completed 3 or 4 grades) [Primary]",
            Q101RUS == 3 ~ "3. Incomplete general secondary education (completed 8 or 9 grades) [Primary]",
            Q101RUS == 4 ~ "4. Complete general secondary education (diploma after completing 11(10) grades) [Primary]",
            Q101RUS == 5 ~ "5. Primary professional education (PTU, lyceum) (1-2 years of training) [Primary]",
            Q101RUS == 6 ~ "6. Secondary professional education (technikum, ushilishe, college, etc.) (3-4 years of training) [Primary]",
            Q101RUS == 7 ~ "7. Incomplete higher education (completed at least 3 grades of university/college) [Primary]",
            Q101RUS == 8 ~ "8. Completed higher education on 5-6 grades on-tier system (diploma of specialist), bachelor or master degree on new 2-tier [College]",
            Q101RUS == 9 ~ "9. Postgraduate education (aspirantura, ordinatura, adjuktura), scientific degree (candidate,doctor of science) [College]",
            Q101RUS == 10 ~ "10. Other [Primary]",
            
            Q101TAJ == 1 ~ "1. Incomplete base-general education (completed less than 9 grades under new system or less than 8 grades under old system) [No education]",
            Q101TAJ == 2 ~ "2. Base-general education (completed 9 grades under new system or 8 grades under old system) [Primary]",
            Q101TAJ == 3 ~ "3. General secondary education (completed 11 grades under new system or 10 grades under old system) [Primary]",
            Q101TAJ == 4 ~ "4. Special secondary education (completed 2 or 3 years of college) [Primary]",
            Q101TAJ == 5 ~ "5. Incomplete higher education (completed 2 years of university studies under new system or 3 years under old system) [Primary]",
            Q101TAJ == 6 ~ "6. Higher education (completed 4 or more years of university studies under new system or 5 or more years under old system) [College]",
            
            Q101THA == 1 ~ "1. None/No formal education [No education]",
            Q101THA == 2 ~ "2. Primary school or below (currently attending primary school or completed 6 grades or less in primary school) [Primary]",
            Q101THA == 3 ~ "3. Lower level secondary school (currently attending secondary school or completed 9 grades or less in secondary school) [Primary]",
            Q101THA == 4 ~ "4. High school (currently attending high school or completed 12 grades or less in high school) [Primary]",
            Q101THA == 5 ~ "5. Diploma/Vocational (currently attending vocational school or completed 3 or 5 years of vocational school after lower lev [Primary]",
            Q101THA == 6 ~ "6. Bachelor degree or higher  (completed 4 or more years of university) [College]",
            
            Q101TUN == 1 ~ "1. Without education/illiterate [No education]",
            Q101TUN == 2 ~ "2. Primary school [Primary]",
            Q101TUN == 3 ~ "3. College school [Primary]",
            Q101TUN == 4 ~ "4. Secondary school [Primary]",
            Q101TUN == 5 ~ "5. University [College]",
            
            Q101TUR == 1 ~ "1. No formal education [No education]",
            Q101TUR == 2 ~ "2. Incomplete primary school (completed less than 5 grades) [No education]",
            Q101TUR == 3 ~ "3. Complete primary school (completed 5 grades) [Primary]",
            Q101TUR == 4 ~ "4. Complete primary education (completed 8 grades) [Primary]",
            Q101TUR == 5 ~ "5. Complete junior high school or vocational school at the same level [Primary]",
            Q101TUR == 6 ~ "6. Complete high school or vocational school at the same level [Primary]",
            Q101TUR == 7 ~ "7. Higher education (completed 2 years of college or 4 years of university studies) [College]",
            Q101TUR == 8 ~ "8. Master (completed 2 more years after university) [College]",
            Q101TUR == 9 ~ "9. PhD (completed 4-6 more years after university) [College]",
            
            Q101UZB == 1 ~ "1. No formal education [No education]",
            Q101UZB == 2 ~ "2. Incomplete primary education (completed less than 4 grades under new system or less than 3 grades under old system) [No education]",
            Q101UZB == 3 ~ "3. Primary education (completed 4 grades under new system or 3 grades under old system) [Primary]",
            Q101UZB == 4 ~ "4. Incomplete base-general (completed less than 9 grades under new system or less than 8 grades under old system) [Primary]",
            Q101UZB == 5 ~ "5. Base-general education (completed 9 grades under new system or 8 grades under old system) [Primary]",
            Q101UZB == 6 ~ "6. General secondary education (completed 11 grades under new system or 10 grades under old system) [Primary]",
            Q101UZB == 7 ~ "7. Special secondary education (completed 2 or 3 years of college) [Primary]",
            Q101UZB == 8 ~ "8. Incomplete higher education (completed less than 4 years of university studies under new system or less than 5 years und [Primary]",
            Q101UZB == 9 ~ "9. Higher education (completed 4 or more years of university studies under new system or 5 or more years under old system) [College]",
            TRUE ~ NA_character_),

      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female = 
          case_when(
            Q95 == 1 ~ 0,
            Q95 == 2 ~ 1,
            TRUE ~ NA_real_),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural =
          case_when(
            Q133 %in% c(2) ~ 1, # rural
            Q133 %in% c(1) ~ 0, # urban 
            TRUE ~ NA_real_),      
    
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q37; QTEXT: How comfortable would you be if a son of yours someday married a Christian?; ROPTIONS: 1 = Very comfortable [=0] + 2 =	Somewhat comfortable [=0] + 3 =	Not too comfortable [=1] + 4 =	Not at all comfortable [=1]; TARGET: Christian; TYPE: Distance, family",

      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(Q37),
            "5" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            Q37 %in% c(1:2) ~ 0,
            Q37 %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q37IRN; QTEXT: Suppose you heard that someone's son married a Christian. Would you totally approve, somewhat approve, somewhat disapprove or totally disapprove of such a marriage?; ROPTIONS: 1 = Totally approve [=0] + 2 =	Somewhat approve [=0] + 3 =	Somewhat disapprove [=1] + 4 = Totally disapprove [=1]; TARGET: Christian; TYPE: Distance, family",

      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(Q37IRN),
            "5" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            Q37IRN %in% c(1:2) ~ 0,
            Q37IRN %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),    
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q37THA; QTEXT: How comfortable would you be if a son of yours someday married a Buddhist?; ROPTIONS: 1 = Very comfortable [=0] + 2 =	Somewhat comfortable [=0] + 3 =	Not too comfortable [=1] + 4 = Not at all comfortable [=1]; TARGET: Buddhist; TYPE: Distance, family",

      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(Q37THA),
            "5" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            Q37THA %in% c(1:2) ~ 0,
            Q37THA %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 4 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_4_qinfo = "NUM: Q38; QTEXT: How comfortable would you be if a daughter of yours someday married a Christian?; ROPTIONS: 1 = Very comfortable [=0] + 2 =	Somewhat comfortable [=0] + 3 =	Not too comfortable [=1] + 4 = Not at all comfortable [=1]; TARGET: Christian; TYPE: Distance, family",

      # original response (as character vector)
        resp_soc_dist_4_original = 
          dplyr::recode(
            as.character(Q38),
            "5" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_4_bin_recode = 
          case_when(
            Q38 %in% c(1:2) ~ 0,
            Q38 %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 5 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_5_qinfo = "NUM: Q38IRN; QTEXT: Suppose you heard that someone's daughter married a Christian. Would you totally approve, somewhat approve, somewhat disapprove or totally disapprove of such a marriage?; ROPTIONS: 1 = Totally approve [=0] + 2 = Somewhat approve [=0] + 3 = Somewhat disapprove [=1] + 4 = Totally disapprove [=1]; TARGET: Christian; TYPE: Distance, family",

      # original response (as character vector)
        resp_soc_dist_5_original = 
          dplyr::recode(
            as.character(Q38IRN),
            "5" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_5_bin_recode = 
          case_when(
            Q38IRN %in% c(1:2) ~ 0,
            Q38IRN %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 6 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_6_qinfo = "NUM: Q38THA; QTEXT: How comfortable would you be if a daughter of yours someday married a Buddhist?; ROPTIONS: 1 = Very comfortable [=0] + 2 = Somewhat comfortable [=0] + 3 = Not too comfortable [=1] + 4 = Not at all comfortable [=1]; TARGET: Buddhist; TYPE: Distance, family",

      # original response (as character vector)
        resp_soc_dist_6_original = 
          dplyr::recode(
            as.character(Q38THA),
            "5" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_6_bin_recode = 
          case_when(
            Q38THA %in% c(1:2) ~ 0,
            Q38THA %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA, # checked; no general trust question

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ###### RELIGIOSITY ######  
    #########################
    
        resp_religiosity_qinfo = "NUM: 61 [Q61]; QTEXT: People practice their religion in different ways. Outside of attending religious services, do you pray several times a day, once a day, a few times a week, once a week, a few times a month, seldom, or never?; ROPTIONS: 1 = Several times a day + 2 = Once a day + 3 = A few times a week + 4 = Once a week + 5 = A few times a month + 6 = Seldom + 7 = Never",
    
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(Q61),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (7 - resp_religiosity_original)/6
    
    ) %>% 
    select(starts_with("resp_"))
```

# Save data

```{r}
  saveRDS(pewwm_clean, "../cleaned-data/y-17-multi-pew-worlds-muslims.rds")
```
